home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13255 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.8 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with a very weird C problem
  5. Date: Fri, 05 Apr 96 15:30:59 GMT
  6. Organization: none
  7. Message-ID: <828718259snz@genesis.demon.co.uk>
  8. References: <31620c87.184254741@199.171.83.2> <4k2tdl$ft4@preeda.internex.net.au>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4k2tdl$ft4@preeda.internex.net.au>
  15.            sultan@connexus.apana.org.au "Jon Hornstein" writes:
  16.  
  17. >With the briefest of looks at the code this function 
  18. >
  19. >
  20. >>char *PkunzipDir(char *curdir,char *name2,char *file)
  21. >>{
  22. >>char pkunzip2[120] = "pkunzip ";
  23. >>strcat(pkunzip2,curdir);
  24. >>strcat(pkunzip2,name2);
  25. >>strcat(pkunzip2," ");
  26. >>strcat(pkunzip2,curdir);
  27. >>strcat(pkunzip2,file);
  28. >>return(pkunzip2);
  29. >>}
  30. >
  31. >should read
  32. >
  33. >
  34. >char *PkunzipDir(char *curdir,char *name2,char *file)
  35. >{
  36. >static char pkunzip2[120] = "pkunzip ";
  37. >
  38. >    strcat(pkunzip2,curdir);
  39. >    strcat(pkunzip2,name2);
  40. >    strcat(pkunzip2," ");
  41. >    strcat(pkunzip2,curdir);
  42. >    strcat(pkunzip2,file);
  43. >    return(pkunzip2);
  44. >}
  45.  
  46. That would fail if you called PkunzipDir() more than once.
  47.  
  48. Or you could write it more simply and clearly (and correctly) as:
  49.  
  50. char *PkunzipDir(char *curdir,char *name2,char *file)
  51. {
  52.     static char pkunzip2[120];
  53.  
  54.     sprintf(pkunzip2, "pkunzip %s%s %s%s", curdir, name2, curdir, file);
  55.  
  56.     return pkunzip2;
  57. }
  58.  
  59. sprintf is extremely useful for string handling.
  60.  
  61. It is generally better to have the calling function pass a pointer to a
  62. suitable buffer than to return a pointer to a static buffer.
  63.  
  64. -- 
  65. -----------------------------------------
  66. Lawrence Kirby | fred@genesis.demon.co.uk
  67. Wilts, England | 70734.126@compuserve.com
  68. -----------------------------------------
  69.